package com.port.schedule.task;

import com.port.dao.BaseValue;
import com.port.schedule.ScheduleService;
import com.port.utils.DataHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;

/**
 * 端口检测task
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class deviceCheck implements ScheduleService {
    private final static HashMap<String, BaseValue> BaseValue = new HashMap<>();

    {
        BaseValue.put("1", new BaseValue("3949446.09", "636122.34", "646.25", "35:39:55.04797N", "112:30:12.03088E"));
        BaseValue.put("2", new BaseValue("3949435.09", "636120.83", "646.35", "35:39:54.69193N", "112:30:11.96388E"));
        BaseValue.put("3", new BaseValue("3949884.30", "636054.86", "675.99", "35:40:09.29492N", "112:30:09.61504E"));
        BaseValue.put("4", new BaseValue("3949828.88", "636145.58", "651.27", "35:40:07.45207N", "112:30:13.18729E"));
        BaseValue.put("5", new BaseValue("3949810.64", "636149.47", "651.46", "35:40:06.85862N", "112:30:13.33078E"));
        BaseValue.put("7", new BaseValue("3949654.67", "636323.55", "581.69", "35:40:01.71340N", "112:30:20.15505E"));
        BaseValue.put("8", new BaseValue("3949881.50", "636040.87", "676.21", "35:40:09.21095N", "112:30:09.05716E"));
    }

    @Override
    public void everyHalfHour() {
//        log.info("坪上煤矿数据开始造假");
//        //向本地csv文件写入内容
//        String filePath = "E:\\data\\pingshangmeikuang\\CDMonitorSession.csv";
//        try (BufferedWriter writer = Files.newBufferedWriter(
//                Paths.get(filePath),
//                StandardCharsets.UTF_8,
//                StandardOpenOption.APPEND)) {
//            writer.write(GetHandler());
//            log.info("Content written to file successfully.");
//        } catch (IOException e) {
//            log.error("Error writing to file: " + e.getMessage());
//        }


    }

    /**
     * 坪上煤矿数据构造
     *
     * @return 数据
     */

    private String GetHandler() {
        StringBuilder result = new StringBuilder();

        //数据时间处理
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime old = now.minus(30, ChronoUnit.MINUTES);
        String nowStr = now.format(formatter);
        String yearMonthDay = nowStr.split(" ")[0];  //数据年月日
        String nowHourMinute = nowStr.split(" ")[1];  //数据收集结束时间
        String oldStr = old.format(formatter);
        String oldHourMinute = oldStr.split(" ")[1];  //数据收集开始时间


        //包装数据
        for (String key : BaseValue.keySet()) {
            BaseValue baseValue = BaseValue.get(key);
            result.append(yearMonthDay).append(",").append(key).append(",").append("JZ").append(",").append(oldHourMinute).append(".00,").append(nowHourMinute).append(".00,").append("60,").append("2,").append("0.0000,").append(baseValue.getLat()).append(",").append(baseValue.getLon()).append(",").append(baseValue.getX()).append(DataHandler.GetRandomNum()).append(",").append(baseValue.getY()).append(DataHandler.GetRandomNum()).append(",").append(baseValue.getZ()).append(DataHandler.GetRandomNum()).append("\r\n");
        }

        return result.toString();
    }
}
